﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Microsoft.Live.ServerControls.VE
{
    class LatLongConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(
            ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(
            ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext 
            context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                return new LatLong();
            }

            if (value is string)
            {
                string s = (string)value;
                if (s.Length == 0)
                {
                    return new LatLong();
                }

                string[] parts = s.Split(' ');

                if ((parts.Length < 2) || (parts.Length > 4))
                {
                    throw new ArgumentException(
                        "Name must have 2 or 3 or 4 parts.", "value");
                }

                if (parts.Length == 2)
                {
                    return new LatLong(Convert.ToDouble(parts[0]), Convert.ToDouble(parts[1]));
                }

                if (parts.Length == 3)
                {
                    return new LatLong(Convert.ToDouble(parts[0]), Convert.ToDouble(parts[1]), Convert.ToDouble(parts[2]));
                }

                if (parts.Length == 4)
                {
                    return new LatLong(Convert.ToDouble(parts[0]), Convert.ToDouble(parts[1]), Convert.ToDouble(parts[2]), (AltitudeMode)Enum.Parse(typeof(AltitudeMode), parts[3]));
                }
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(
            ITypeDescriptorContext context,
            CultureInfo culture, object value, Type destinationType)
        {
            if (value != null)
            {
                if (!(value is LatLong))
                {
                    throw new ArgumentException(
                        "Invalid LatLong", "value");
                }
            }

            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return String.Empty;
                }

                LatLong point = (LatLong)value;

                    return String.Format("{0} {1} {2} {3}",
                        point.Latitude,
                        point.Longitude,
                        point.Altitude, point.AltitudeMode);
 
            }

            return base.ConvertTo(context, culture, value, 
                destinationType);
        }


    }
}
